package com.gin.kotlin.no_gay_alliance.adapter

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.ReplyListActivity
import com.gin.kotlin.no_gay_alliance.activity.UserCenterActivity
import com.gin.kotlin.no_gay_alliance.argument.ReplyListArgument
import com.gin.kotlin.no_gay_alliance.argument.ReplyListArgument.Type
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.ViewUtils.Companion.visible
import com.gin.kotlin.no_gay_alliance.database.entity.ReportHistory
import com.gin.kotlin.no_gay_alliance.databinding.ItemReportHistoryBinding
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils.Companion.zdtIntoTextView

class ReportHistoryAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemReportHistoryBinding, ReportHistory>(context, handler),
    View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemReportHistoryBinding>, position: Int, b: ItemReportHistoryBinding, dataModel: ReportHistory) {
        b.tvTitle.text = dataModel.title
        b.tvContent.text = dataModel.content

        b.tvReason.visibility = visible(dataModel.reason)
        b.tvReason.text = SpannableStringBuilder().also { builder ->
            builder.append("理由: ")
            builder.append(dataModel.reason)
            val span = ForegroundColorSpan(getAttrColor(R.attr.textColorRed))
            builder.setSpan(span, builder.length - dataModel.reason.length, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        b.tvUsername.text = SpannableStringBuilder().also { builder ->
            builder.append("被举报人: ")
            builder.append(dataModel.username)
            val span = ForegroundColorSpan(getAttrColor(R.attr.textColorBlue))
            builder.setSpan(span, builder.length - dataModel.username.length, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        zdtIntoTextView(context, dataModel.timestamp, b.tvTimestamp, getAttrColor(R.attr.textColor))

        listOf(b.tvTitle, b.tvContent, b.tvReason, b.tvUsername, b.tvTimestamp).forEach {
            it.tag = dataModel
            it.setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
        val reportHistory = v?.tag as? ReportHistory ?: return
        when (v.id) {
            R.id.tv_title -> ReplyListActivity.startActivity(context, ReplyListArgument(Type.LIST, topicId = reportHistory.topicId))
            R.id.tv_username -> UserCenterActivity.startActivity(context, reportHistory.userId)
            else ->
                reportHistory.replyId?.also { ReplyListActivity.startActivity(context, ReplyListArgument(Type.SINGLE, replyId = it)) }
                    ?: ReplyListActivity.startActivity(context, ReplyListArgument(Type.LIST, topicId = reportHistory.topicId))

        }
    }
}